package com.corner.model.cornerservice.helper;


import com.corner.model.cornerservice.domain.Conversation;
import com.corner.model.cornerservice.domain.User;
import com.corner.model.cornerservice.service.Service;
import com.corner.model.cornerservice.service.exception.BaseException;
import com.corner.model.cornerservice.utils.ServiceQuery;

import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2015/7/28，14.27.
 * 描述：
 */
public class ConversationHelper {

    /**
     * 获取同感人数 通过result返回数量
     */
    public static Observable<Conversation> getConversation(final Long fromUserId, final Long toUserId) {
        return Observable.create(new Observable.OnSubscribe<Conversation>() {
            @Override
            public void call(Subscriber<? super Conversation> subscriber) {
                try {
                    ServiceQuery query = new ServiceQuery();
                    query.equal(Conversation.MAIN_USER, fromUserId, ServiceQuery.FOREIGN_KEY);
                    query.equal(Conversation.CUSTOM_USER, toUserId, ServiceQuery.FOREIGN_KEY);
                    query.setLimit(1);
                    List<Conversation> conversations = Service.getInstance().conversationService
                            .list(query.toString());
                    if (conversations != null && conversations.size() > 0) {
                        subscriber.onNext(conversations.get(0));
                    } else {
                        query = new ServiceQuery();
                        query.equal(Conversation.MAIN_USER, toUserId, ServiceQuery.FOREIGN_KEY);
                        query.equal(Conversation.CUSTOM_USER, fromUserId, ServiceQuery.FOREIGN_KEY);
                        query.setLimit(1);
                        conversations = Service.getInstance().conversationService
                                .list(query.toString());
                        if (conversations != null && conversations.size() > 0) {
                            subscriber.onNext(conversations.get(0));
                        } else {
                            subscriber.onNext(null);
                        }
                    }
                } catch (BaseException e) {
                    e.printStackTrace();
                    subscriber.onError(new Throwable());
                }
            }
        }).subscribeOn(Schedulers.io());
    }

    public static Observable<Conversation> save(final String conversationId, final Long objectId, final Long friendId) {

        return Observable.create(new Observable.OnSubscribe<Conversation>() {
            @Override
            public void call(Subscriber<? super Conversation> subscriber) {
                try {
                    Conversation conversation = new Conversation();
                    conversation.setConversationId(conversationId);
                    User main = new User();
                    main.setObjectId(objectId);
                    conversation.setMainUser(main);
                    User customUser = new User();
                    customUser.setObjectId(friendId);
                    conversation.setCustomUser(customUser);
                    conversation.setMainIsInBlackList(false);
                    conversation.setCustomIsInBlackList(false);
                    Service.getInstance().conversationService.save(conversation);
                    subscriber.onNext(null);
                } catch (BaseException e) {
                    e.printStackTrace();
                }
            }
        }).subscribeOn(Schedulers.io());
    }
}
